import {isEmpty, resetState} from '@/utils/index';
import {mapState, mapGetters, mapMutations, mapActions} from 'vuex';

/*
公共函数模块的混合
 */
var fn = {
    methods: {
        /**
         * 函数防抖 (只执行最后一次点击)
         * @param fn
         * @param delay
         * @returns {Function}
         * @constructor
         */
        DeBounce(fn, delay=500){
            let timer;
            return function () {
                let args = arguments;
                if(timer){
                    clearTimeout(timer);
                }
                timer = setTimeout(() => {
                    timer = null;
                    fn.apply(this, args);
                }, delay);
            }
        },
            /**
             * 函数节流
             * @param fn
             * @param interval
             * @returns {Function}
             * @constructor
             */
            Throttle(fn, interval=500) {
                let last;
                let timer;
                return function () {
                    let args = arguments;
                    let now = +new Date();
                    if (last && now - last < interval) {
                        clearTimeout(timer);
                        timer = setTimeout(() => {
                            last = now;
                            fn.apply(this, args);
                        }, interval);
                    } else {
                        last = now;
                        fn.apply(this, args);
                    }
                }
            }
    },
}
export default fn;
